<template>
  <div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="showDia">
      <!-- 表单内容 -->
      <el-form ref="formdatt" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDia">取消</el-button>
          <el-button type="primary" @click="trueAdd">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addPermissionApi } from '@/api/premission'
export default {
  props: {
    showDialog: {
      type: Boolean
    }
  },
  data() {
    return {
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    showDia() {
      this.$emit('update:showDialog', false)
      this.$refs.formdatt.resetFields()
    },
    trueAdd() {
      this.$refs.formdatt.validate(async(flag) => {
        if (flag) {
          const res = await addPermissionApi(this.formData)
          console.log(res)
          this.$message.success('添加成功')
          this.showDia()
          this.$parent.UserList()
        }
      })
    }
  }
}
</script>

<style>

</style>
